package com.bwie.controller;

import com.bwie.config.JwtConfig;
import com.bwie.domain.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.MobileLoginVo;
import com.bwie.vo.PageInfoVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2024/11/12 16:44
 */

@RestController
@RequestMapping("/user/user")
@Slf4j
public class TbUserController {

    @Autowired
    TbUserService tbUserService;

    @Autowired
    JwtConfig jwtConfig;

    @GetMapping(value = "/getJwtKey")
    public R getJwtKey(){
        System.out.println("jwtKey: " + jwtConfig.getKey());
        return R.OK(jwtConfig.getKey());
    }

    /**
     * 根据用户ID获取用户信息
     * @param userId
     * @return
     */
    @PostMapping(value = "/getUserById/{userId}")
    public R getUserById(@PathVariable("userId") Integer userId){
        TbUser tbUser = tbUserService.getUserById(userId);
        if(tbUser == null) {
            return R.ERROR(404, "用户不存在");
        }

        return R.OK(tbUser);
    }

    /**
     * 用户登录
     * @param loginInfoVo
     * @return
     */
    @PostMapping(value = "/login")
    public R login(@RequestBody LoginInfoVo loginInfoVo) {
        return tbUserService.login(loginInfoVo);
    }

    /**
     * 商家注册接口
     * @return
     */
    @PostMapping(value = "/shopperRegister")
    public R shopperRegister(@Validated @RequestBody TbUser tbUser) {
        return tbUserService.shopperRegister(tbUser);
    }

    /**
     * 修改商户信息
     * @param tbUser
     * @return
     */
    @PostMapping(value = "/updateShopper")
    public R updateShopper(@RequestBody TbUser tbUser) {
        return tbUserService.updateShopper(tbUser);
    }

    /**
     * 发送验证码
     * @param mobile
     * @return
     */
    @PostMapping(value = "/sendCode/{mobile}")
    public R sendCode(@PathVariable("mobile") String mobile) {
        return tbUserService.sendCode(mobile);
    }

    /**
     * 商户登录
     * @param mobileLoginVo
     * @return
     */
    @PostMapping(value = "/shopperLogin")
    public R shopperLogin(@Validated @RequestBody MobileLoginVo mobileLoginVo){
        return tbUserService.shopperLogin(mobileLoginVo);
    }

    /**
     * 分页获取商家列表
     * @return
     */
    @PostMapping(value = "/getShopperList")
    public R getShopperList(@Validated @RequestBody PageInfoVo pageInfoVo) {
        return tbUserService.getShopperList(pageInfoVo);
    }

    @PostMapping(value = "/getUserInfo/{userId}")
    public R getUserInfo(@PathVariable("userId") Integer userId) {
        return tbUserService.getUserInfo(userId);
    }

    /**
     * 审核商户
     * @param tbUser
     * @return
     */
    @PostMapping(value = "/checkShopper")
    public R checkShopper(@RequestBody TbUser tbUser) {
        return tbUserService.checkShopper(tbUser);
    }

    /**
     * 删除商户
     * @param userId
     * @return
     */
    @PostMapping(value = "/deleteShopper/{userId}")
    public R deleteShopper(@PathVariable("userId") Integer userId) {
        return tbUserService.deleteShopper(userId);
    }

    /**
     * 批量删除商户
     * @param tbUsers
     * @return
     */
    @PostMapping(value = "/deleteShoppers")
    public R deleteShoppers(@RequestBody List<TbUser> tbUsers) {
        return tbUserService.deleteShoppers(tbUsers);
    }

    /**
     * 获取最近一周的注册人数
     * @return
     */
    @PostMapping(value = "/getNumberForWeek")
    @ApiOperation(value = "获取最近一周的注册人数")
    public R getNumberForWeek(){
        return tbUserService.getNumberForWeek();
    }
}
